package com.su.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.su.tools.DaoSupport1;
import com.su.tools.PageData;

import net.sf.json.JSONObject;

@Service("deptIntroduceService")
public class DeptIntroduceService {

	@Resource(name = "daoSupport1")
	private DaoSupport1 dao;
	private JSONObject json = new JSONObject();
	private PageData retPd;
	private List<PageData> retListPd;

	public JSONObject invoke(PageData pd) throws Exception {
		if (pd.getString("params").equals("DT101")) {
			this.DT101(pd);
		} else if (pd.getString("params").equals("DT102")) {
			this.DT102(pd);
		} else if (pd.getString("params").equals("DT103")) {
			this.DT103(pd);
		} else if (pd.getString("params").equals("DT104")) {
			this.DT104(pd);
		} else if (pd.getString("params").equals("DT105")) {
			this.DT105(pd);
		}  else {
			json.put("code", "100");
			json.put("msg", "没有找到该业务代码");
			return json;
		}
		return json;
	}

	/**
	 * 获取科室动态列表
	 * 
	 * @param pd
	 */
	private void DT101(PageData pd) throws Exception {

		retListPd = dao.getList("DeptIntroduceMapper.countDept", pd);
		int total = retListPd.size();
		retListPd = dao.getList("DeptIntroduceMapper.getDeptList", pd);
		json.put("total", total);
		json.put("rows", retListPd);
	}

	/**
	 * 保存科室
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DT102(PageData pd) throws Exception {
		String action = pd.getString("action");
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateNow = sdf.format(d);
		pd.put("CREATE_DATE", dateNow);
		HttpServletRequest request = pd.getRequest();
		HttpSession session = request.getSession();
		String CZRY = session.getAttribute("session_user").toString();
		pd.put("CZRY", CZRY);
		int rows = 0;
		if (action.equals("add")) {
			rows = dao.insert("DeptIntroduceMapper.insertDept", pd);
		} else {
			rows = dao.update("DeptIntroduceMapper.updateDept", pd);
		}
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}

	/**
	 * 查询科室
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DT103(PageData pd) throws Exception {
		retPd = dao.getOne("DeptIntroduceMapper.getDeptOne", pd);
		if (retPd != null) {
			json.put("status", "true");
			json.put("result", retPd);
		} else {
			json.put("status", "false");
			json.put("result", retPd);
		}
	}

	/**
	 * 删除科室
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DT104(PageData pd) throws Exception {
		String codeList = pd.getString("codeList");
		String[] codeArray = codeList.split(",");
		int rows = dao.delete("DeptIntroduceMapper.delDept", codeArray);
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}
	
	/**
	 * 获取科室信息（无分页）
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void DT105(PageData pd) throws Exception {
		retListPd = dao.getList("DeptIntroduceMapper.getDeptAll", pd);
		if(retListPd.size()>0){
			json.put("status", "true");
			json.put("rows", retListPd);
		}else{
			json.put("status", "false");
			json.put("rows", retListPd);
		}
	}
}
